<!-- Load TinyMCE -->
<script type="text/javascript" src="media/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="media/tiny_mce/config.js"></script>
<script type="text/javascript">
	tiny('detail');
	$(function(){
		$('.preview').click(function(){
			tinyMCE.triggerSave();
		})
		
			
			
			$('select').after(' <span class="option"><a rel="edit" href="#" >แก้ไข</a> <a rel="add" href="#" >เพิ่ม</a> <a rel="del" href="#" >ลบ</a></span>');
			
			$('.option a[rel=edit]').live('click',function(){
				var select = $(this).parent().prev();
				select.before('<span class="option_edit"><input rel="'+ select.find('option:selected').val() +'" type="text" name="name" value="'+ select.find('option:selected').text() + '" /> \
				<a rel="save" href="#" >แก้ไข</a> <a rel="cancel" href="#" >ยกเลิก</a></span>').hide();
				$('span.option').hide();
				return false;
			})
			
			$('.option a[rel=add]').live('click',function(){
				var select = $(this).parent().prev();
				select.before('<span class="option_edit"><input type="text" name="name" value="" /> \
				<a rel="save" href="#" >เพิ่ม</a> <a rel="cancel" href="#" >ยกเลิก</a></span>').hide();
				$('span.option').hide();
				return false;
			})
			
			$('.option a[rel=del]').live('click',function(){
				var select = $(this).parent().prev();
				var url = 'contents/admin/contents/ddl_delete/' + select.attr('name');
				if(confirm('ยืนยันการลบข้อมูล')){
					$.post(url,{'id':select.val()},function(data){
						select.before(data).remove();
					})
				}
				return false;
			})
			
			$('.option_edit a[rel=cancel]').live('click',function(){
				var select = $(this).parent().parent().find('select');
				select.show().prev().remove();
				$('span.option').show();
				return false;
			})
			
			$('.option_edit a[rel=save]').live('click',function(){
			var input = $(this).parent().find('input[name=name]');
			var select = $(this).parent().parent().find('select');
			var url = 'contents/admin/contents/ddl/' + select.attr('name');
			var id = input.attr('rel');
			var value = input.val();
			
			if (typeof id !== 'undefined' && id !== false) {
				json = {'id':id,'name':value};
			}else{
				json = {'name':value};
			}
			
			
			
			$.post(url,json,function(data){
				select.before(data).remove();
				$('span.option_edit').remove();
				$('span.option').show();
			})
			return false;
		})
			
			$("input[name=tag]").tagEditor({
				<?php if($content->tag):?>
				items: '<?php echo $content->tag?>'.split(","),
				<?php endif; ?>
				continuousOutputBuild:true
				});	
	})
</script>
<h1><?php echo admin_breadcrumb(get_breadcrumb($category->id,TRUE)) ?></h1>
<form action="contents/admin/contents/save/<?php echo $content->id ?>" method="post" enctype="multipart/form-data" >
	
<table class="form">
	<?php if(is_file('uploads/content/thumbnail/'.$content->image)): ?>
	<tr><th></th><td><img class="img" src="<?php echo 'uploads/content/thumbnail/'.$content->image ?>"  /></td></tr>
	<?php endif ?>
	<tr><th>รูปภาพ </th><td><input type="file" name="image" /></td></tr>
	<tr><th>เริ่ม </th><td><input type="text" name="start_date" value="<?php echo DB2Date($content->start_date)?>" class="datepicker" /></td></tr>
	<tr><th>สิ้นสุด </th><td><input type="text" name="end_date" value="<?php echo DB2Date($content->end_date)?>" class="datepicker" /></td></tr>
	<tr>
		<th>ประเภทหนังสือ</th>
		<td>
			<?php echo form_dropdown('book_type_id',get_option('id', 'name', 'book_types',' order by name'),$content->book_type_id) ?>
		</td>
	</tr>
	<tr><th>แท็ก </th><td><small>กด Enter เพื่อเพิ่มแท็ก</small><br /><input type="text" name="tag" value="" /></td></tr>
	<tr>
		<th>URL </th>
		<td>
			<input type="text" name="url" value="<?php echo $content->url?>" class="full" />
		</td>
	</tr>
	<tr>
		<th>หัวข้อ </th>
		<td>
			<input type="text" name="title" value="<?php echo $content->title?>" class="full" />
		</td>
	</tr>
	<tr><th>ไฟล์เอกสาร </th>
		<td>
			<small>อนุญาติเฉพาะ  pdf, xls, xlsx, doc, docx, ppt, pptx, rar และ zip</small>
			<br /><input type="file" name="file" />
			<?php if($content->file): ?>
			 <a href="contents/admin/contents/download/<?php echo $content->id ?>">ดาวน์โหลด</a> 
			 <a href="contents/admin/contents/delete_file/<?php echo $content->id ?>">ลบไฟล์</a>
			 <?php endif; ?>
		</td>
	</tr>
	<tr>
		<th>บทคัดย่อ </th>
		<td>
			<textarea name="intro" class="full"><?php echo $content->intro ?></textarea>
		</td>
	</tr>
	<tr>
		<th>รายละเอียด </th>
		<td>
			<textarea name="detail" class="full tinymce"><?php echo $content->detail ?></textarea>
		</td>
	</tr>
	<tr>
		<th></th>
		<td>
			<?php echo form_hidden('category_id',$category->id)?>
			<?php echo form_submit('',lang('btn_submit'),'class="button"')?>
			<?php echo form_button('',lang('btn_back'),'class="button" onclick="window.location = \'contents/admin/contents/index/'.$category->id.'\'"')?> 
			หรือ <?php echo anchor('contents/admin/contents/preview/?iframe=true&width=90%&height=90%','พรีวิว','rel="lightbox" class="preview"') ?>
		</td></tr>
</table>
</form>